package com.kuangstudy.controller.geo;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: xuke
 * @time: 2021/6/7 22:29
 */
@RestController
@Log4j2
public class GeoController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/init")
    public void init() {
        // 1：创建一个位置集合
        Map<String, Point> postitions = new HashMap<>();
        postitions.put("梅州区政府", new Point(116.08760839643578, 24.272291058018723));
        postitions.put("盘古花园", new Point(116.09472486185275, 24.276664296367017));
        postitions.put("梅州森林公园", new Point(116.08096114851881, 24.28089400501498));
        postitions.put("富泰花园", new Point(116.0898762810192, 24.281467513865618));
        postitions.put("金沙湾大酒店", new Point(116.0898762810192, 24.281467513865618));
        // 存放进geo中
        this.redisTemplate.opsForGeo().add("geo:positions", postitions);
    }

    @GetMapping("/position")
    public List<Point> position(String member) {
        List<Point> position = this.redisTemplate.opsForGeo().position("geo:positions", member);
        return position;
    }

    @GetMapping("/hash")
    public List<Point> hash(String member) {
        List<Point> position = this.redisTemplate.opsForGeo().hash("geo:positions", member);
        return position;
    }

    // 测试两个位置直接的直线距离
    @GetMapping("/distance")
    public Distance distance(String member1, String member2) {
        Distance distance = this.redisTemplate.opsForGeo().distance("geo:positions", member1, member2, RedisGeoCommands.DistanceUnit.KILOMETERS);
        return distance;
    }

    // 通过经度和维度查找附近(distancenum)km以内的10条数据
    @GetMapping("/radiusbyxy")
    public GeoResults radiusbyxy(int distancenum) {
        Distance distance = new Distance(distancenum, RedisGeoCommands.DistanceUnit.KILOMETERS);
        // 1:这个坐标是 梅江区税务局
        Circle circle = new Circle(new Point(116.1296001782314, 24.309622314899958), distance);
        // 2:查询返回对应的50条数据
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                .includeDistance().includeCoordinates().sortAscending().limit(10);
        GeoResults geoResults = this.redisTemplate.opsForGeo().radius("geo:positions", circle, args);
        return geoResults;
    }

    // 通过地址查找（distanct）KM附近的10条数据
    @GetMapping("/radiusbymember")
    public GeoResults radiusbymember(String member, int distanct) {
        // 1:查询返回对应的50条数据
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                .includeDistance().includeCoordinates().sortAscending().limit(10);
        //2:查询半径是10Km的
        Distance distance = new Distance(distanct, Metrics.KILOMETERS);
        GeoResults geoResults = this.redisTemplate.opsForGeo().radius("geo:positions", member, distance, args);
        return geoResults;
    }

    // 删除元素
    @GetMapping("/remove")
    public Long remove(String member) {
        Long remove = this.redisTemplate.opsForGeo().remove("geo:positions", member);
        return remove;
    }
}